/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package p2p.raca.Client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 *
 * @author bayer
 */
public class Client {
    public static boolean block = true;
    public static Lista L;
    public static ListaFileClient LF;
    private static String Server;
    public static PeerFileInformation PFI;
    private static ClientIssueRequest CIR;
    private static ClientListener CL;
    public Client()
    {
        Server ="x-men";
        L = new Lista();
        LF = new ListaFileClient();
    }
    public void printMenu()
    {
        System.out.println("*******************************");
        System.out.println("**********Bem vindo************");
        System.out.println("*****Cliente P2P FileShare*****");
        System.out.println("*******************************");
        System.out.println("    Opcoes de utilizacao:   \n");
        System.out.println("1. Listar arquivos da rede.");
        System.out.println("2. Listar Hosts ativos.");
        System.out.println("3. Informacao sobre o arquivo");
        System.out.println("4. Baixar Arquivo");
        System.out.println("5. Atualizar os arquivos no servidor");
        System.out.println("9. Sair.");
        System.out.println("*******************************");
        System.out.print("Opcao:");
    }    	
    public static void main(String[] args) throws IOException, InterruptedException {
        
        Client C = new Client();
        C.printMenu();
        CL = new ClientListener();
        CL.start();
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));  
        String entrada = in.readLine();  
        while (!entrada.equals("9"))
        {
            if (entrada.equals("1"))
                {
                    CIR = new ClientIssueRequest(Server, 5050, "getListaFile");
                    CIR.start();
                    while(CIR.isAlive())
                        Thread.sleep(200);
                    System.out.println("Recebi");
                    LF.LoadLista();
                    LF.PrintLista();
                
            }else if (entrada.equals("2"))
                {
                    CIR = new ClientIssueRequest(Server, 5050, "getLista");
                    CIR.start();
                    while(CIR.isAlive())
                        Thread.sleep(200);
                    L.LoadLista();
                    L.PrintNomes();               
                }
            else if (entrada.equals("3"))
                {
                    System.out.println("Entre com o nome do arquivo para obter informaçoes");
                    String nome;
                    nome = in.readLine();
                    CIR = new ClientIssueRequest(Server, 5050, "getPFI"+nome);
                    CIR.start();
                    while(CIR.isAlive())
                        Thread.sleep(200);
                     PFI = new PeerFileInformation(nome);
                     PFI.printPFI();
            }
            else if (entrada.equals("4"))
                {
                    System.out.println("Entre com o nome do arquivo para baixar");
                    String nome;
                    nome = in.readLine();
                    CIR = new ClientIssueRequest(Server, 5050, "getPFI"+nome);
                    CIR.start();
                    while(CIR.isAlive())
                        Thread.sleep(200);
                    System.out.println(CIR.isAlive());
                    PFI = new PeerFileInformation(nome);
                    PFI.printPFI();
                    boolean baixou = false;
                    System.out.println("PFI size = "+PFI.getWhoHasSize());
                    for (int i=0; i<PFI.getWhoHasSize() || !baixou;i++)
                    {
                        CIR = new ClientIssueRequest(PFI.getHost(i), 5056, "donwload"+nome);
                        CIR.size = PFI.fileSize;
                        CIR.start();
                        while(CIR.isAlive())
                            Thread.sleep(200);
                        baixou = true;
                    }
                    CIR = new ClientIssueRequest(Server, 5050, "updateFiles");
                    CIR.start();
                    
            }else if (entrada.equals("5"))
                {
                     CIR = new ClientIssueRequest(Server, 5050, "updateFiles");
                     CIR.start();
            }
           C.printMenu();
           entrada = in.readLine();
        }
       }
}